package com.gzdh.dishrecognition.ui.activity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dhkj.web.bean.JiaoBanRes;
import com.dhkj.web.bean.SysUserModel;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.gzdh.dishrecognition.MainActivity;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.SysConstConfig;
import com.gzdh.dishrecognition.ui.adapter.SelectPosAdapter;
import com.gzdh.dishrecognition.ui.base.BaseMVCActivity;
import com.gzdh.dishrecognition.ui.dialog.MyTimePickerDialog;
import com.gzdh.dishrecognition.util.SystemUtils;
import com.gzdh.dishrecognition.util.print.PrintUtil;

import net.posprinter.posprinterface.TaskCallback;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class jiaobanActivity extends BaseMVCActivity{
    @Override
    protected int getLayoutId() {
        return R.layout.activity_jiaoban;
    }

    private String startTime = "";
    @SuppressLint("SimpleDateFormat")
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private Context context;

    private JiaoBanRes jiaoBanRes;

    private SelectPosAdapter selectPosAdapter;

    @Override
    protected void initView() {
        findViewById(R.id.backBut).setOnClickListener(view -> finish());
        context = this;
        TextView startDataBut = findViewById(R.id.startDataBut);
        startDataBut.setText(sdf.format(new Date()));
        startDataBut.setOnClickListener(view -> {
            Calendar mcalendar = Calendar.getInstance(); // 获取当前时间 — 年、月、日
            int year = mcalendar.get(Calendar.YEAR); // 得到当前年
            int month = mcalendar.get(Calendar.MONTH); // 得到当前月
            final int day = mcalendar.get(Calendar.DAY_OF_MONTH); // 得到当前日
            // 日期选择对话框
            new DatePickerDialog(context, (view1, year1, month1, dayOfMonth) ->
                    startDataBut.setText(year1 + "-" + String.format("%02d", month1 + 1) + "-" + String.format("%02d", dayOfMonth))
                    , year, month, day)
                    .show(); // 弹出日历对话框时，默认显示 年，月，日
        });

        TextView startTimeBut = findViewById(R.id.startTimeBut);
        startTimeBut.setText("00:00:00");
        startTimeBut.setOnClickListener(view -> {
            MyTimePickerDialog.OnTimeSetListener listener =
                    (timeView, hourOfDay, minute, seconds) -> startTimeBut.setText(String.format("%02d", hourOfDay) +
                    ":" + String.format("%02d", minute) +
                    ":" + String.format("%02d", seconds));
            new MyTimePickerDialog(this, listener, 0, 0, 0, true).show();
        });

        TextView endDataBut = findViewById(R.id.endDataBut);
        endDataBut.setText(sdf.format(new Date()));
        endDataBut.setOnClickListener(view -> {
            Calendar mcalendar = Calendar.getInstance(); // 获取当前时间 — 年、月、日
            int year = mcalendar.get(Calendar.YEAR); // 得到当前年
            int month = mcalendar.get(Calendar.MONTH); // 得到当前月
            final int day = mcalendar.get(Calendar.DAY_OF_MONTH); // 得到当前日
            // 日期选择对话框
            new DatePickerDialog(context, (view12, year12, month12, dayOfMonth) ->
                    endDataBut.setText(year12 + "-" + String.format("%02d", month12 + 1) + "-" + String.format("%02d", dayOfMonth)),
                    year, month, day)
                    .show(); // 弹出日历对话框时，默认显示 年，月，日
        });

        TextView endTimeBut = findViewById(R.id.endTimeBut);
        endTimeBut.setText("23:59:59");
        endTimeBut.setOnClickListener(view -> {
            MyTimePickerDialog.OnTimeSetListener listener =
                    (timeView, hourOfDay, minute, seconds) -> endTimeBut.setText(String.format("%02d", hourOfDay) +
                            ":" + String.format("%02d", minute) +
                            ":" + String.format("%02d", seconds));
            new MyTimePickerDialog(this, listener, 23, 59, 59, true).show();
        });

        SharedPreferences systemI = getSharedPreferences("systemI", MODE_PRIVATE);
        String role = systemI.getString("role", "");
        if(!"订餐员".equals(String.valueOf(role).trim())) {
            findViewById(R.id.select_pos).setVisibility(View.VISIBLE);
            ListView listView = findViewById(R.id.posNum_list);
            listView.setVisibility(View.VISIBLE);
            RequestUtils.getPosNumList(this, systemI.getString("addressNo", ""), new MyObserverRes<>(this) {
                @Override
                public void onSuccess(List<SysUserModel> result) {
                    selectPosAdapter = new SelectPosAdapter(result, context);
                    listView.setAdapter(selectPosAdapter);
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    Toast.makeText(context, errorMsg, Toast.LENGTH_SHORT).show();
                }
            });

            Button checkAll = findViewById(R.id.check_all);
            checkAll.setText("全选");
            checkAll.setOnClickListener(view -> selectPosAdapter.checkAll());
            Button clearPos = findViewById(R.id.clear_pos);
            clearPos.setText("清空");
            clearPos.setOnClickListener(view -> selectPosAdapter.initData());
        }
        LinearLayout pjLinear = findViewById(R.id.pjLinear);
        TextView startTimeTv = findViewById(R.id.startTimeTv);
        TextView endTimeTv = findViewById(R.id.endTimeTv);
        TextView printTimeTv = findViewById(R.id.printTimeTv);
        TextView zCountTv = findViewById(R.id.zCountTv);
        TextView zAmountTv = findViewById(R.id.zAmountTv);

        TextView zgCount1Tv = findViewById(R.id.zgCount1Tv);
        TextView zgAmount1Tv = findViewById(R.id.zgAmount1Tv);

        TextView zgCount2Tv = findViewById(R.id.zgCount2Tv);
        TextView zgAmount2Tv = findViewById(R.id.zgAmount2Tv);

        TextView zZgCountsTv = findViewById(R.id.zZgCountsTv);
        TextView zZgAmountTv = findViewById(R.id.zZgAmountTv);

        TextView btCount1Tv = findViewById(R.id.btCount1Tv);
        TextView btAmount1Tv = findViewById(R.id.btAmount1Tv);

        TextView btCount2Tv = findViewById(R.id.btCount2Tv);
        TextView btAmount2Tv = findViewById(R.id.btAmount2Tv);

        TextView zBtCountsTv = findViewById(R.id.zBtCountsTv);
        TextView zBtAmountTv = findViewById(R.id.zBtAmountTv);

        TextView wxCount1Tv = findViewById(R.id.wxCount1Tv);
        TextView wxAmount1Tv = findViewById(R.id.wxAmount1Tv);

        TextView wxCount2Tv = findViewById(R.id.wxCount2Tv);
        TextView wxAmount2Tv = findViewById(R.id.wxAmount2Tv);

        TextView zWxCountsTv = findViewById(R.id.zWxCountsTv);
        TextView zWxAmountTv = findViewById(R.id.zWxAmountTv);

        TextView cashCounts1Tv = findViewById(R.id.cashCounts1Tv);
        TextView cashAmount1Tv = findViewById(R.id.cashAmount1Tv);

        TextView cashCounts2Tv = findViewById(R.id.cashCounts2Tv);
        TextView cashAmount2Tv = findViewById(R.id.cashAmount2Tv);

        TextView zCashCountsTv = findViewById(R.id.zCashCountsTv);
        TextView zCashAmountTv = findViewById(R.id.zCashAmountTv);

        TextView lsCounts1Tv = findViewById(R.id.lsCounts1Tv);
        TextView lsAmount1Tv = findViewById(R.id.lsAmount1Tv);

        TextView lsCounts2Tv = findViewById(R.id.lsCounts2Tv);
        TextView lsAmount2Tv = findViewById(R.id.lsAmount2Tv);

        TextView zLsCountsTv = findViewById(R.id.zLsCountsTv);
        TextView zLsAmountTv = findViewById(R.id.zLsAmountTv);

        TextView countsTv = findViewById(R.id.countsTv);
        TextView amountTv = findViewById(R.id.amountTv);

        TextView userNameTv = findViewById(R.id.userNameTv);

        findViewById(R.id.subBut).setOnClickListener(view -> {
            List<String> posNumList;
            if(!"订餐员".equals(String.valueOf(role).trim())) {
                posNumList = selectPosAdapter.getPosNum();
                if(posNumList.isEmpty()) {
                    Toast.makeText(context, "请选择机号", Toast.LENGTH_SHORT).show();
                    return;
                }
            } else {
                posNumList = new ArrayList<>();
                posNumList.add(SysConstConfig.getInstance().getUser());
            }
            RequestUtils.getJiaoBan(this, SystemUtils.getSerialNumber(),
                    startDataBut.getText().toString() + " " + startTimeBut.getText().toString(),
                    endDataBut.getText().toString() + " " + endTimeBut.getText().toString(),
                    posNumList, new MyObserverRes<>(this) {
                        @Override
                        public void onSuccess(JiaoBanRes result) {
                            System.out.println("获取：" + result);
                            result.setUserName(systemI.getString("username", ""));
                            startTimeTv.setText(String.format("开始时间：%s", result.getStartTime()));
                            endTimeTv.setText(String.format("结束时间：%s", result.getEndTime()));
                            printTimeTv.setText(String.format("打印时间：%s", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
                            zCountTv.setText(String.valueOf(result.getZcounts()));
                            zAmountTv.setText(result.getZamount());

                            zgCount1Tv.setText(String.valueOf(result.getZgCounts1()));
                            zgAmount1Tv.setText(result.getZgAmount1());

                            zgCount2Tv.setText(String.valueOf(result.getZgCounts2()));
                            zgAmount2Tv.setText(result.getZgAmount2());

                            zZgCountsTv.setText(String.valueOf(result.getzZgCounts()));
                            zZgAmountTv.setText(result.getzZgAmount());

                            btCount1Tv.setText(String.valueOf(result.getBtCounts1()));
                            btAmount1Tv.setText(result.getBtAmount1());

                            btCount2Tv.setText(String.valueOf(result.getBtCounts2()));
                            btAmount2Tv.setText(result.getBtAmount2());

                            zBtCountsTv.setText(String.valueOf(result.getzBtCounts()));
                            zBtAmountTv.setText(String.valueOf(result.getzBtAmount()));

                            wxCount1Tv.setText(String.valueOf(result.getWxCounts1()));
                            wxAmount1Tv.setText(result.getWxAmount1());

                            wxCount2Tv.setText(String.valueOf(result.getWxCounts2()));
                            wxAmount2Tv.setText(result.getWxAmount2());

                            zWxCountsTv.setText(String.valueOf(result.getZwxCounts()));
                            zWxAmountTv.setText(result.getZwxAmount());

                            cashCounts1Tv.setText(String.valueOf(result.getCashCounts1()));
                            cashAmount1Tv.setText(result.getCashAmount1());

                            cashCounts2Tv.setText(String.valueOf(result.getCashCounts2()));
                            cashAmount2Tv.setText(result.getCashAmount2());

                            zCashCountsTv.setText(String.valueOf(result.getZcashCounts()));
                            zCashAmountTv.setText(result.getZcashAmount());

                            lsCounts1Tv.setText(String.valueOf(result.getLsCounts1()));
                            lsAmount1Tv.setText(result.getLsAmount1());

                            lsCounts2Tv.setText(String.valueOf(result.getLsCounts2()));
                            lsAmount2Tv.setText(result.getLsAmount2());

                            zLsCountsTv.setText(String.valueOf(result.getzLsCounts()));
                            zLsAmountTv.setText(result.getzLsAmount());

                            userNameTv.setText(String.format("打印人：%s", result.getUserName()));

                            countsTv.setText(String.valueOf(result.getCounts()));
                            amountTv.setText(result.getAmount());
                            pjLinear.setVisibility(View.VISIBLE);
                            jiaoBanRes = result;
                        }

                        @Override
                        public void onFailure(Throwable e, String errorMsg) {

                        }
                    });
        });

        findViewById(R.id.printBut).setOnClickListener(view -> {
            if (jiaoBanRes != null) {
                prints(jiaoBanRes);
            }

        });

    }

    private void prints(JiaoBanRes jiaoBanRes) {

        try {
            PrintUtil pUtil = new PrintUtil();

            // 店铺名 居中 放大
            pUtil.printAlignment(1);
            pUtil.setTextSize(0);
            pUtil.printLargeText("销售汇总报表");
            pUtil.printLine();
            pUtil.printAlignment(0);
            pUtil.printLine();
            pUtil.printTwoColumn("开始时间:", jiaoBanRes.getStartTime());
            pUtil.printLine();
            pUtil.printTwoColumn("结束时间:", jiaoBanRes.getEndTime());
            pUtil.setTextSize(0);
            pUtil.printLine();
            // 分隔线
            pUtil.printDashLine();
            pUtil.printLine();
            //打印商品列表

            pUtil.printThreeColumn("总销售单据", String.valueOf(jiaoBanRes.getZcounts()), jiaoBanRes.getZamount());
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printThreeColumn("自购消费", String.valueOf(jiaoBanRes.getZgCounts1()), jiaoBanRes.getZgAmount1());
            pUtil.printLine();
            pUtil.printThreeColumn("自购退款", String.valueOf(jiaoBanRes.getZgCounts2()), jiaoBanRes.getZgAmount2());
            pUtil.printLine();
            pUtil.printThreeColumn("自购营业额", String.valueOf(jiaoBanRes.getzZgCounts()), jiaoBanRes.getzZgAmount());
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printThreeColumn("补贴消费", String.valueOf(jiaoBanRes.getBtCounts1()), jiaoBanRes.getBtAmount1());
            pUtil.printLine();
            pUtil.printThreeColumn("补贴退款", String.valueOf(jiaoBanRes.getBtCounts2()), jiaoBanRes.getBtAmount2());
            pUtil.printLine();
            pUtil.printThreeColumn("补贴营业额", String.valueOf(jiaoBanRes.getzBtCounts()), jiaoBanRes.getzBtAmount());
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printThreeColumn("微信消费", String.valueOf(jiaoBanRes.getWxCounts1()), jiaoBanRes.getWxAmount1());
            pUtil.printLine();
            pUtil.printThreeColumn("微信退款", String.valueOf(jiaoBanRes.getWxCounts2()), jiaoBanRes.getWxAmount2());
            pUtil.printLine();
            pUtil.printThreeColumn("微信营业额", String.valueOf(jiaoBanRes.getZwxCounts()), jiaoBanRes.getZwxAmount());
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printThreeColumn("现金消费", String.valueOf(jiaoBanRes.getCashCounts1()), jiaoBanRes.getCashAmount1());
            pUtil.printLine();
            pUtil.printThreeColumn("现金退款", String.valueOf(jiaoBanRes.getCashCounts2()), jiaoBanRes.getCashAmount2());
            pUtil.printLine();
            pUtil.printThreeColumn("现金营业额", String.valueOf(jiaoBanRes.getZcashCounts()), jiaoBanRes.getZcashAmount());
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printThreeColumn("聚合消费", String.valueOf(jiaoBanRes.getLsCounts1()), jiaoBanRes.getLsAmount1());
            pUtil.printLine();
            pUtil.printThreeColumn("聚合退款", String.valueOf(jiaoBanRes.getLsCounts2()), jiaoBanRes.getLsAmount2());
            pUtil.printLine();
            pUtil.printThreeColumn("聚合营业额", String.valueOf(jiaoBanRes.getzLsCounts()), jiaoBanRes.getzLsAmount());
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printThreeColumn("合计营业额", String.valueOf(jiaoBanRes.getCounts()), jiaoBanRes.getAmount());
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printText("打印人：" + jiaoBanRes.getUserName());
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printText("打印时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            pUtil.printLine();
            pUtil.setTextSize(0);
            pUtil.printDashLine();
            pUtil.printLine(4);
            pUtil.printQZ();
            if (!MainActivity.ISCONNECT || MainActivity.myBinder == null) {
                System.out.println("没有驱动，退出");
                return;
            }
            if (MainActivity.ISCONNECT) {
                MainActivity.myBinder.Write(pUtil.getOutbyte(), new TaskCallback() {
                    @Override
                    public void OnSucceed() {

                    }

                    @Override
                    public void OnFailed() {

                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void dissmissPresnetation() {

    }
}
